/* UVA394 LA5311 POJ1557 Mapmaker */
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int L = 10 + 1;
const int N = 10 + 1;
struct Array
{
    char name[L];
    int base;
    int size;
    int dim;
    int l[L];
    int u[L];
    int c[L];
} a[N];

int n, r;

int getid(char *s)
{
    for (int i = 1; i <= n; i++)
        if (strcmp(a[i].name, s) == 0)
            return i;
    return -1;
}

int main()
{
    scanf("%d%d", &n, &r);
    for (int i = 1; i <= n; i++)
    {
        scanf("%s%d%d%d", a[i].name, &a[i].base, &a[i].size, &a[i].dim);
        for (int j = 1; j <= a[i].dim; j++)
            scanf("%d%d", &a[i].l[j], &a[i].u[j]);

        int d = a[i].dim;
        a[i].c[d] = a[i].size;
        for (int j = d - 1; j >= 1; j--)
            a[i].c[j] = a[i].c[j + 1] * (a[i].u[j + 1] - a[i].l[j + 1] + 1);

        int b = a[i].base;
        for (int j = 1; j <= d; j++)
            b -= a[i].c[j] * a[i].l[j];
        a[i].c[0] = b;
    }

    char name[L];
    int id[N];
    for (int i = 1; i <= r; i++)
    {
        scanf("%s", name);
        int ix = getid(name);
        int d = a[ix].dim;
        for (int j = 1; j <= d; j++)
            scanf("%d", &id[j]);

        int ans = a[ix].c[0];
        for (int j = 1; j <= d; j++)
            ans += a[ix].c[j] * id[j];

        printf("%s[", name);
        for (int j = 1; j <= d; j++)
        {
            printf("%d", id[j]);
            if (j != d)
                printf(", ");
        }
        printf("] = %d\n", ans);
    }

    return 0;
}
